package com.lwj.liwanjia.view.activity.work.shang_jia;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.WoDeShouRuAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.WorkBean07;
import com.lwj.liwanjia.config.URLConstant;
import com.lwj.liwanjia.utils.BaseUtils;
import com.lwj.liwanjia.utils.SpfUtils;

import java.io.IOException;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by 001 on 2017/9/15.
 */

public class ShouRuXiangQingActivity extends BaseActivity implements View.OnClickListener {

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what) {

                case 0:
                    Toast.makeText(ShouRuXiangQingActivity.this, "数据有误", Toast.LENGTH_LONG).show();
                    break;

                case 1:

                    if (!TextUtils.isEmpty(photoUrl)) {
                        Glide.with(ShouRuXiangQingActivity.this).load(photoUrl).error(R.mipmap.touxiang).into(circle_photo);
                    }

                    if (!TextUtils.isEmpty(name)) {
                        tv_name.setText(name);
                    }

                    if (!TextUtils.isEmpty(money)) {
                        tv_money.setText(money+"元");
                    }

                    if (!TextUtils.isEmpty(fangSHi)) {
                        tv_fangShi.setText(fangSHi);
                    }

                    if (!TextUtils.isEmpty(zhangHao)) {
                        tv_zhangHao.setText(zhangHao);
                    }

                    if (!TextUtils.isEmpty(timeResult)) {
                        tv_time.setText(timeResult);
                    }

                    if (!TextUtils.isEmpty(dingDanHao)) {
                        tv_dingDanHao.setText(dingDanHao);
                    }



                    break;
            }
        }
    };

    private TextView tv_back;
    private TextView tv_title;
    private CircleImageView circle_photo;//头像
    private TextView tv_name;//姓名
    private TextView tv_money;//金额
    private TextView tv_fangShi;//方式
    private TextView tv_zhangHao;//账号
    private TextView tv_time;//时间
    private TextView tv_dingDanHao;//订单号

    private String photoUrl;
    private String name;
    private String money;
    private String fangSHi;
    private String zhangHao;
    private String timeResult;
    private String dingDanHao;

    @Override
    protected void initView() {

        tv_back = (TextView) findViewById(R.id.tv_left);
        tv_title = (TextView) findViewById(R.id.tv_title);

        circle_photo = (CircleImageView) findViewById(R.id.circle_photo);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_money = (TextView) findViewById(R.id.tv_money);
        tv_fangShi = (TextView) findViewById(R.id.tv_fangShi);
        tv_zhangHao = (TextView) findViewById(R.id.tv_zhangHao);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_dingDanHao = (TextView) findViewById(R.id.tv_dingDanHao);

        tv_back.setVisibility(View.VISIBLE);
        tv_title.setVisibility(View.VISIBLE);
        tv_title.setText("我的收入");

        tv_back.setOnClickListener(this);
    }

    @Override
    protected void initData() {

        Intent intent = getIntent();
        String id = intent.getStringExtra("id");

        //请求网络 获取数据
        initOkhttp(id);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shou_ru_xiang_qing;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.tv_left:
                finish();
                break;
        }
    }

    //请求网络 获取数据
    private void initOkhttp(String id) {

        String url = URLConstant.BASE_URL + "shop/rewardInfo";
        String token = SpfUtils.getSpfUtils(this).getToken();

        OkHttpClient mOkHttpClient=new OkHttpClient();
        RequestBody formBody = new FormBody.Builder()
                .add("token",token)
                .add("oneCardItemId",id)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Call mCall= mOkHttpClient.newCall(request);
        mCall.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //Log.d("Tag", "分类数据获取失败");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //Log.d("Tag", "分类图数据获取成功");

                String result = null;

                try {
                    result = response.body().string();
                    //Log.d("Tag",result);
                } catch (Exception e) {
                }

                parse01(result);
            }
        });
    }

    //解析
    private void parse01(String result) {

        Gson gson = new Gson();
        WorkBean07 bean = gson.fromJson(result, WorkBean07.class);

        if (bean != null) {

            switch (bean.getState()) {

                case 0:
                    mHandler.sendEmptyMessage(0);
                    break;

                case 1:

                    photoUrl = bean.getData().getHead();
                    name = bean.getData().getNick();
                    money = bean.getData().getPayNum();
                    fangSHi = bean.getData().getPayWay()+"";
                    zhangHao = bean.getData().getUserName();

                    long time = bean.getData().getTime();
                    timeResult = BaseUtils.getString(String.valueOf(time / 1000));

                    dingDanHao = bean.getData().getOrderId();

                    mHandler.sendEmptyMessage(1);

                    break;
            }
        }
    }
}
